package com.awspaas.user.apps.cpq.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;

public class CPQ_GetTransportPriceWeb {

	public String getTransportPrice(UserContext userContext, String bindId) {
		try {
			BO data=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG").bindId(bindId).detail();
			List<BO> dataGrids=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_GRIDS").bindId(bindId).list();
			double ztj=0;
			double zzl=0;
			for (BO bo : dataGrids) {
				String tj=bo.getString("TJ");
				String ZL=bo.getString("ZL");
				ztj=ztj+Double.parseDouble(tj);
				zzl=zzl+Double.parseDouble(ZL);
			}
			BO transportData=new BO();
			transportData.set("FHD", data.getString("FHDZ"));//发货地
			transportData.set("MDD", data.getString("MDD_S")+data.getString("MDD_S_S"));//目的地
			transportData.set("ZLCS", data.getString("LCS"));//总里程数
			transportData.set("ZZL", zzl);//总重量
			transportData.set("ZTJ",ztj);//总体积
			transportData.set("LS",data.getString("LS") );//列数
			transportData.set("CS", data.getString("CS"));//层数
			transportData.set("DLZS",data.getString("MLZS") );//单列组数
			transportData.set("GSMC", data.getString("GSMC"));//公司名称
			transportData.set("XMBH", data.getString("XMMC"));//项目名称
			transportData.set("GLXMBJDH", data.getString("APPLYNO"));//关联项目报价单号
			//获取车辆信息
			List<BO> transportCars=SDK.getBOAPI().query("BO_EU_CPQ_TRANSPORT_MODEL").addQuery("STATUS=", "启用").list();
			List<BO> grids=new ArrayList<BO>();
			if(transportCars.size()>0) {
				for (BO bo : transportCars) {
					BO grid=new BO();
					grid.set("LX",bo.getString("YSGJLX") );
					grid.set("CX",bo.getString("CX")  );
					grid.set("ZZ", bo.getString("ZZL") );
					grid.set("TJ",bo.getString("TJ")  );
					grid.set("CD", bo.getString("CD"));
					grid.set("KD", bo.getString("KD"));
					grid.set("GD", bo.getString("GD"));
					grid.set("GLDJ", bo.getString("GLDJ") );
					grid.set("GLS", data.getString("LCS"));
					double price=Double.parseDouble(bo.getString("GLDJ"))*Double.parseDouble(data.getString("LCS"));
					grid.set("DTYF", price);
                    grids.add(grid);
                  		
				}				
			}
			//创建项目运输成本记录清单实例
			ProcessInstance processInstance=SDK.getProcessAPI().createBOProcessInstance("obj_f3b110aea7b441dabdb2bc6f379df8ea", userContext.getUID(), "");			
			SDK.getBOAPI().create("BO_EU_CPQ_TRANSPORT_PRICE", transportData, processInstance.getId(), userContext.getUID());
			SDK.getBOAPI().create("BO_EU_CPQ_TRANSPORT_PRICE_PZ_GRID", grids, processInstance.getId(), userContext.getUID());
			ResponseObject ro = ResponseObject.newOkResponse();
			Map<String, String> map=new HashMap<String, String>();
			map.put("bindId", processInstance.getId());
			ro.setData(map);
			return ro.toString();
		} catch (Exception e) {
			throw  e;
		}
		
	}

}
